﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.APIs.Cards;
using Waker.Common;

namespace Waker.Cards
{
    public partial class FormStaffCard : Base.FormBasePop
    {
        DataTable _DtSource;
        string _EmpId;
        public FormStaffCard(string id)
        {
            InitializeComponent();
            _DtSource = new BAL.BAL_Cards().GetEmpCardByEmp_ID(id);
            _EmpId = id;
            //if (_DtSource.Rows.Count < 1)
            //    this.BindingContext[_DtSource].AddNew();
            this.Text += string.Format("[{0}]", _DtSource.Rows[0]["Emp_Name"]);
        }

        private void FormStaffCard_Load(object sender, EventArgs e)
        {
            if (CardHelper.RC500USB_init() == 0)
            {
                lblStatus.Text = "已连接";
                CardHelper.RC500USB_buzzer(1, 25, 25, 1);
            }
            else
                lblStatus.Text = "未连接";
            ControlUtil.BindData(this.Controls, _DtSource);
            if (dteCard_ExpireDate.EditValue == null || dteCard_ExpireDate.EditValue == DBNull.Value)
                dteCard_ExpireDate.DateTime = DateTime.Now.AddYears(1);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                //var f = new FormCardSetting();
                //f.ShowDialog();
                //return;
                var isok = false;
                byte type = 0;
                UInt32 snr = 0;
                for (int i = 0; i < 10; i++)
                {
                    var b = CardHelper.RC500USB_request(0, out type);
                    if (b == 0)//对第一个读卡器进行操作
                    {
                        isok = true;
                        break;
                    }
                }
                if (!isok)
                {
                    UI.ShowError("获取卡号失败");
                    txtNewCardNo.Text = string.Empty;
                    return;
                }
                if (CardHelper.RC500USB_anticoll(0, out snr) != 0)//进行防碰撞选择成功则返回卡号
                {
                    UI.ShowError("获取卡号失败");
                    txtNewCardNo.Text = string.Empty;
                    return;
                }
                CardHelper.RC500USB_buzzer(1, 25, 25, 1);
                txtNewCardNo.Text = snr.ToString();
            }
            catch (Exception ex)
            {
                UI.ShowError(ex.Message);
            }
        }
        protected override void FormSave()
        {
            try
            {
                if (_DtSource == null || _DtSource.Rows.Count < 1)
                    return;
                base.errorPop.ClearErrors();

                if (string.IsNullOrEmpty(txtNewCardNo.Text))
                {
                    base.errorPop.SetError(txtNewCardNo, "新卡号不能为空");
                    return;
                }

                if (dteCard_ExpireDate.EditValue == DBNull.Value || dteCard_ExpireDate.EditValue == null)
                {
                    base.errorPop.SetError(dteCard_ExpireDate, "请输入有效日期");
                    return;
                }
                StartWaiting();
                this.BindingContext[_DtSource].EndCurrentEdit();

                var bal = new BAL.BAL_Cards();
                //if (_DtSource.Rows[0]["Card_No"] == DBNull.Value)
                //{
                if (!bal.CheckEmployeeCardNo(txtNewCardNo.Text))
                {
                    UI.ShowInfo("卡号已经存在,请重新输入");
                    return;
                }
                _DtSource.Rows[0]["Card_No"] = txtNewCardNo.Text.Trim();
                _DtSource.Rows[0]["Emp_ID"] = _EmpId;
                //}
                //讲发卡信息上传到门禁设备上 同时将结果保存到IsUpLoad

                var isOk = bal.SaveEmployeeCard(_DtSource.Rows[0]);
                EndWaiting();
                if (isOk)
                {
                    UI.ShowInfo("保存成功");
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    UI.ShowError("保存失败");
                }
            }
            catch (Exception ex)
            { UI.ShowError(ex.Message); }
            finally
            {
                EndWaiting();
            }
        }
    }
}
